```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解Java悲观锁机制</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .code-block {
            background-color: #2d2d2d;
            border-radius: 0.5rem;
        }
        .mermaid {
            background-color: white;
            padding: 1rem;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 3.5rem;
            line-height: 1;
            margin: 0.15em 0.1em 0 0;
            color: #6e8efb;
            font-weight: bold;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">深入理解Java悲观锁机制</h1>
                <p class="text-xl md:text-2xl mb-8 max-w-3xl font-serif">探索synchronized关键字背后的"悲观"哲学及其在多线程环境中的应用</p>
                <div class="flex space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">#Java并发</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">#线程安全</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">#synchronized</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Intro Section -->
        <section class="mb-16">
            <div class="drop-cap text-lg leading-relaxed text-gray-700">
                <p><span class="text-indigo-600 font-bold">synchronized</span> 关键字被称为"悲观锁"主要是因为它的工作原理和设计思路基于一种"悲观"假设：<strong>认为在多线程环境中对共享资源的访问可能会发生冲突，因此需要通过锁机制来预防这种冲突</strong>。确保通过锁机制来防止多线程冲突。在实现同步时，线程会被阻塞直到获得锁，这种方式是基于对潜在冲突的防范。相对而言，乐观锁则基于对冲突发生概率较低的假设，采用不同的机制来处理并发访问。</p>
            </div>
        </section>

        <!-- Key Points -->
        <section class="grid md:grid-cols-2 gap-8 mb-16">
            <!-- Card 1 -->
            <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                <div class="flex items-center mb-4">
                    <div class="bg-indigo-100 p-3 rounded-full mr-4">
                        <i class="fas fa-lock text-indigo-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800">锁的基本机制</h3>
                </div>
                <ul class="space-y-3 text-gray-700">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                        <span><strong>悲观假设：</strong>在多线程环境中，<code>synchronized</code> 假设多个线程可能同时尝试访问同一个资源，因此必须使用锁来防止数据竞争和状态不一致。</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                        <span><strong>独占访问：</strong><code>synchronized</code> 确保在同一时刻只有一个线程可以执行被锁保护的代码块或方法，其他线程必须等待，直到当前线程释放锁。</span>
                    </li>
                </ul>
            </div>

            <!-- Card 2 -->
            <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                <div class="flex items-center mb-4">
                    <div class="bg-purple-100 p-3 rounded-full mr-4">
                        <i class="fas fa-cogs text-purple-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800">锁的特性</h3>
                </div>
                <ul class="space-y-3 text-gray-700">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                        <span><strong>阻塞：</strong>如果一个线程试图获取已经被其他线程持有的锁，它会被阻塞，直到锁被释放。这个特性表现出悲观锁的特征。</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                        <span><strong>自动管理：</strong><code>synchronized</code> 自动处理锁的获取和释放，确保在进入临界区时持有锁，在退出临界区时释放锁，无需额外的操作。</span>
                    </li>
                </ul>
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2 font-serif">悲观锁 vs 乐观锁</h2>
            <div class="bg-white p-6 rounded-xl shadow-md">
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-xl font-bold mb-4 text-indigo-600 flex items-center">
                            <i class="fas fa-shield-alt mr-2"></i> 悲观锁
                        </h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-angle-right text-indigo-500 mt-1 mr-2"></i>
                                <span>假设冲突一定会发生，先获取锁再访问资源</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-angle-right text-indigo-500 mt-1 mr-2"></i>
                                <span>实现简单，直接阻塞其他线程</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-angle-right text-indigo-500 mt-1 mr-2"></i>
                                <span>适合写操作多的场景</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-angle-right text-indigo-500 mt-1 mr-2"></i>
                                <span>示例：<code>synchronized</code>, <code>ReentrantLock</code></span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h3 class="text-xl font-bold mb-4 text-green-600 flex items-center">
                            <i class="fas fa-leaf mr-2"></i> 乐观锁
                        </h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-angle-right text-green-500 mt-1 mr-2"></i>
                                <span>假设冲突很少发生，先操作再检查冲突</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-angle-right text-green-500 mt-1 mr-2"></i>
                                <span>通常使用版本号或CAS实现</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-angle-right text-green-500 mt-1 mr-2"></i>
                                <span>适合读操作多的场景</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-angle-right text-green-500 mt-1 mr-2"></i>
                                <span>示例：<code>AtomicInteger</code>, <code>CAS</code></span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Implementation Details -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2 font-serif">synchronized的实现细节</h2>
            <div class="bg-white p-6 rounded-xl shadow-md">
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-xl font-bold mb-4 text-gray-800">同步方式</h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-code text-indigo-500 mt-1 mr-2"></i>
                                <span><strong>同步方法：</strong>在方法声明中使用<code>synchronized</code>关键字</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-code text-indigo-500 mt-1 mr-2"></i>
                                <span><strong>同步代码块：</strong>使用<code>synchronized(object)</code>语法</span>
                            </li>
                        </ul>
                    </div>
                    <div>
                        <h3 class="text-xl font-bold mb-4 text-gray-800">锁对象</h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-key text-indigo-500 mt-1 mr-2"></i>
                                <span>实例方法锁的是当前实例对象</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-key text-indigo-500 mt-1 mr-2"></i>
                                <span>静态方法锁的是类的Class对象</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Example -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2 font-serif">悲观锁代码示例</h2>
            <div class="code-block p-6 rounded-xl overflow-x-auto">
                <pre class="text-gray-200"><code class="language-java">
public class PessimisticLockDemo {

    private static final Object lock = new Object();

    public void criticalSection() {
        synchronized (lock) {
            // 临界区代码
            System.out.println("线程 " + Thread.currentThread().getName() + " 正在执行临界区代码");
            try {
                Thread.sleep(2000); // 模拟长时间的操作
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    public static void main(String[] args) {
        PessimisticLockDemo demo = new PessimisticLockDemo();
        
        Thread t1 = new Thread(demo::criticalSection);
        Thread t2 = new Thread(demo::criticalSection);
        
        t1.start();
        t2.start();
    }
}
                </code></pre>
            </div>
            <div class="mt-4 bg-blue-50 p-4 rounded-lg">
                <h4 class="font-bold text-blue-800 mb-2 flex items-center">
                    <i class="fas fa-info-circle mr-2"></i> 代码说明
                </h4>
                <ul class="text-blue-800 space-y-2">
                    <li class="flex items-start">
                        <i class="fas fa-angle-right mt-1 mr-2"></i>
                        <span><strong>悲观锁：</strong>在 <code>criticalSection</code> 方法中，<code>synchronized</code> 确保同一时刻只有一个线程可以执行该方法的临界区代码。</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-angle-right mt-1 mr-2"></i>
                        <span><strong>线程阻塞：</strong>当一个线程执行 <code>criticalSection</code> 方法时，另一个线程尝试进入该方法会被阻塞，直到前一个线程释放锁。</span>
                    </li>
                </ul>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 border-b pb-2 font-serif">锁机制可视化</h2>
            <div class="mermaid">
                sequenceDiagram
                    participant T1 as 线程1
                    participant T2 as 线程2
                    participant Lock as 锁对象
                    
                    T1->>Lock: 获取锁
                    activate Lock
                    T1-->>T1: 执行临界区代码
                    T2->>Lock: 尝试获取锁(阻塞)
                    T1-->>Lock: 释放锁
                    deactivate Lock
                    Lock->>T2: 获取锁
                    activate Lock
                    T2-->>T2: 执行临界区代码
                    T2-->>Lock: 释放锁
                    deactivate Lock
            </div>
        </section>

        <!-- Summary -->
        <section class="bg-indigo-50 p-8 rounded-xl">
            <h2 class="text-2xl font-bold mb-4 text-indigo-800 font-serif">核心要点总结</h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <h3 class="font-bold text-lg mb-2 text-indigo-700">悲观锁特点</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-lock text-indigo-500 mt-1 mr-2"></i>
                            <span>基于"冲突预防"的思想</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-hourglass-half text-indigo-500 mt-1 mr-2"></i>
                            <span>获取锁失败会阻塞线程</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-shield-alt text-indigo-500 mt-1 mr-2"></i>
                            <span>适合并发写入多的场景</span>
                        </li>
                    </ul>
                </div>
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <h3 class="font-bold text-lg mb-2 text-indigo-700">synchronized实现</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-cog text-indigo-500 mt-1 mr-2"></i>
                            <span>自动获取和释放锁</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-project-diagram text-indigo-500 mt-1 mr-2"></i>
                            <span>锁粒度可控制在方法或代码块</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-level-up-alt text-indigo-500 mt-1 mr-2"></i>
                            <span>可重入，同一线程可多次获取锁</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-6xl px-4">
            <div class="flex flex-col items-center">
                <div class="mb-4">
                    <h3 class="text-xl font-semibold text-white">技术小馆</h3>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-400 hover:text-white transition duration-300">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```